<?php
session_start(); //Do not remove this
    if ($_SESSION['login']!='123!@#'){
		header('location:index.php');
	}
	require "../../php/config.php";
	require "../../php/libs/db_functions.php";
	 $chapter_name =  isset($_POST['chapter_name'])? $_POST['chapter_name'] : 'noname';
	 $chapter_content =  isset($_POST['chapter_content'])? $_POST['chapter_content'] : 'noname';
	 $story_id =  isset($_POST['story_id'])? $_POST['story_id'] : 1;
	 $story_table_name = 'story_id_'.$story_id;
	//exit();
	  CONNECT_DB();
		mysql_query("SET NAMES utf8");

		$sql="INSERT INTO story_content (story_id, story_chapter_id, story_chapter_name, story_chapter_content) 
				SELECT $story_id, MAX(chapter_id)
				FROM story_content WHERE story_id = $story_id";
		$result=mysql_query($sql);
		/**
			update story_chapter_count
		**/
		$sql="INSERT INTO story_list (story_count_chapter) 
				SELECT MAX(story_chapter_id) + 1,'$chapter_name' 
				FROM $story_table_name";
		$result=mysql_query($sql);
		
		if ($result){
			echo 'successful';
		} else {
			echo 'err - check your story name!';
		}
?>